package lium.lbs.lbsim.activity

import android.content.Intent
import android.os.Bundle
import android.support.v7.widget.GridLayoutManager
import android.support.v7.widget.RecyclerView
import android.text.TextUtils
import android.view.View
import com.buz.hjcdriver.bean.GroupMemberBean
import com.chad.library.adapter.base.BaseQuickAdapter
import com.lmlibrary.Constants
import com.lmlibrary.bean.ResponseBean
import com.lzy.okgo.model.Response
import kotlinx.android.synthetic.main.activity_group_memberlist.*
import lium.lbs.lbsim.R
import lium.lbs.lbsim.adapter.GroupMemberOtherAdapter
import lium.lbs.lbsim.bean.GroupMemberListkt
import lium.lbs.lbsim.jgchat.application.GJApp
import lium.lbs.lbsim.jgchat.utils.photovideo.takevideo.utils.LogUtils
import lium.lbs.lbsim.utils.SpUtils
import lium.lbs.lbsim.utils.callbck.DialogCallback
import lium.lbs.lbsim.zfw.PotentialFriendActivity
import java.util.ArrayList
import java.util.HashMap

class GroupMemberListActivity : BaseActivity(), BaseQuickAdapter.OnItemClickListener {


    lateinit var adapter: GroupMemberOtherAdapter
    var groupId: String = ""
    val bfBean = ArrayList<GroupMemberBean>()
    var pageNo: Int = 1


    override fun getLayoutId(): Int {
        return R.layout.activity_group_memberlist
    }

    override fun initView(savedInstanceState: Bundle?) {
        setTitleWithBack("群成员列表")
        groupId = intent.getStringExtra(GJApp.MY_GROUP_ID)
        LogUtils.e("body-groupId--"+groupId)
        initRecyclerView()
        getDataList()
    }


    fun initRecyclerView() {
        //初始化adapter
        adapter = GroupMemberOtherAdapter()
        val layoutManager = GridLayoutManager(this, 5)
        recyclerView.layoutManager = layoutManager as RecyclerView.LayoutManager?
        recyclerView.adapter = adapter
        adapter.setOnItemClickListener(this)
        adapter.disableLoadMoreIfNotFullPage(recyclerView) //关闭BRAVE的自动加载功能 改成 refreshLayout的上拉加载
        refreshLayout.setOnRefreshListener {
            pageNo = 1
            getDataList()
            refreshLayout.finishRefresh()
            refreshLayout.finishLoadMore()
        }
        refreshLayout.setOnLoadMoreListener {
            ++pageNo
            getDataList()
            refreshLayout.finishRefresh()
            refreshLayout.finishLoadMore()
        }
    }

    fun getDataList() {
        val param = HashMap<String, String>()
        param["memberId"] = SpUtils.getInstance().get(Constants.memberId, "") as String
        param["groupId"] = groupId
        param["pageNo"] = pageNo.toString()
        param["pageSize"] = "60"

        postData(
            "/group/groupMember",
            param,
            object : DialogCallback<ResponseBean<GroupMemberListkt>>(this) {
                override fun onSuccess(response: Response<ResponseBean<GroupMemberListkt>>) {
                    setDataList(response.body().data)
                }
            })
    }

    fun setDataList(groupMemberListkt: GroupMemberListkt) {
        refreshLayout.finishRefresh()
        refreshLayout.finishLoadMore()

        var list = arrayListOf<GroupMemberBean>()
        for (index in 0..groupMemberListkt.memberList.size - 1) {
            list.add(
                GroupMemberBean(
                    if (TextUtils.isEmpty(groupMemberListkt.memberList.get(index).gcard)) {
                        groupMemberListkt.memberList.get(index).nickName
                    } else {
                        groupMemberListkt.memberList.get(index).gcard
                    }
                    ,
                    if (TextUtils.isEmpty(groupMemberListkt.memberList.get(index).headImage)){
                        ""
                    } else{
                        groupMemberListkt.memberList.get(index).headImage
                    },
                    groupMemberListkt.memberList.get(index).memberId
                )
            )
        }
        if (list == null) {
            refreshLayout.finishLoadMoreWithNoMoreData()  //没有数据
            if (pageNo > 1) {
                pageNo--
            }
        } else {
            if (pageNo == 1) {
                adapter!!.data.clear()
                bfBean.clear()
            }
            bfBean.addAll(list)
            list?.let {
                adapter!!.data.addAll(list)
            }
        }
        refreshLayout.finishLoadMoreWithNoMoreData()
        adapter!!.notifyDataSetChanged()
    }

    override fun getNetWorkData() {

    }

    override fun onItemClick(adapter: BaseQuickAdapter<*, *>?, view: View?, position: Int) {
        val intent = Intent(this, PotentialFriendActivity::class.java)
        intent.putExtra("fid", bfBean[position].id)
        startActivity(intent)
    }
}
